/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#ifndef BACKUP_HANDLER_H
#define BACKUP_HANDLER_H

#include "common/JsonHelper.h"
#include "BackintConst.h"

namespace backint {
class BackupHandler {
public:
    BackupHandler();

    ~BackupHandler()
    {}

    uint32_t ExecBackup(const StuParam &inParam, const PlugParams &plugParam);
private:
    uint32_t DataBackup(const StuParam &inParam, const StuFileInfo &stFileInfo);
    uint32_t CataLogBackup(const StuParam &inParam, const StuFileInfo &stFileInfo);
    uint32_t LogBackup(const StuParam &inParam, const StuFileInfo &stFileInfo);
    uint32_t ReadPipeAndFile(const std::string &inputFile, StuFileInfo &stFileInfo);
    uint32_t BackupResult();
    uint32_t WriteDataToFile(const std::string &pipeFile, const std::string &outputFile, int dataSize = -1);
    uint32_t SelectFile(int inFile);
    void ReadWriteData(const StuParam &inParam, const StuFileInfo &fileInfo);
    void GetBackupParam(const StuParam &inParam, const PlugParams &plugParam);
    void LogReadWriteData(const StuParam &inParam, const StuFileInfo &fileInfo);
    void CataLogReadWriteData(const StuParam &inParam, const StuFileInfo &fileInfo);
    void InitAndRegTracePoint();
private:
    using BackupFunc = std::function<uint32_t(const StuParam&, const StuFileInfo&)>;
    std::map<std::string, BackupFunc> m_backupFunMap;
private:
    std::string m_pid;
    std::string m_jobId;
    std::string m_subJobId;
    std::string m_dbName;
    std::string m_cachePath;
    std::string m_dataPath;
    std::string m_backupID;
    std::string m_outputFile;
    std::string m_resultFile;
    std::vector<std::string> m_outputResult;
    Json::Value m_outputValue;
    int m_threadCount = 0;
    bool m_backupResult = true;
    std::mutex m_writeOutFileMutex;
    std::mutex m_threadMutex;
};
}

#endif